#!/bain/bash

Install_Memcached()
{
    echo "====== Installing memcached ======"
    PHP_ZTS="memcached.so"
    zend_ext_dir="/usr/local/php/lib/php/extensions/no-debug-non-zts-20151012/"
    zend_ext=${zend_ext_dir}${PHP_ZTS}
    sed -i '/memcache.so/d' /usr/local/php/etc/php.ini
    sed -i '/memcached.so/d' /usr/local/php/etc/php.ini
    if [ -s "${zend_ext}" ]; then
        rm -f "${zend_ext}"
    fi

    cd ${cur_dir}/src
    wget -cN ${Download_Mirror}/libevent/${Libevent_Ver}-stable.tar.gz
    [[ ! -s ${Libevent_Ver}-stable.tar.gz ]] && wget -c https://github.com/libevent/libevent/releases/download/release-$(echo ${Libevent_Ver} | awk -F"-" '{print $2}' )-stable/${Libevent_Ver}-stable.tar.gz
    tar zxf ${Libevent_Ver}-stable.tar.gz
    cd ${Libevent_Ver}-stable
    ./configure
    make && make install
    ldconfig
    rm -fr ${cur_dir}/src/${Libevent_Ver}-stable

    cd ${cur_dir}/src
    wget -cN ${Download_Mirror}/memcached/${Memcached_Ver}.tar.gz
    [[ ! -f ${Memcached_Ver}.tgz ]] && wget -c http://www.memcached.org/files/${Memcached_Ver}.tar.gz
    tar zxf ${Memcached_Ver}.tar.gz
    cd ${Memcached_Ver}
    ./configure --prefix=/usr/local/memcached
    make &&make install
    rm -fr ${cur_dir}/src/${Memcached_Ver}
    ln -sf /usr/local/memcached/bin/memcached /usr/bin/memcached
    \cp ${cur_dir}/init.d/init.d.memcached /etc/init.d/memcached
    chmod +x /etc/init.d/memcached
    if [ ! -d /var/lock/subsys ]; then
        mkdir -p /var/lock/subsys
    fi
    if [ "$PM" = "yum" ]; then
        chkconfig --add memcached
        chkconfig memcached on
        firewall-cmd --zone=public --add-port=80/tcp --permanent
        firewall-cmd --reload
     elif [ "$PM" = "apt" ]; then
        update-rc.d -f memcached defaults
    fi

    Install_PHPMemcached
    sed -i "/the dl()/i\extension = \"${PHP_ZTS}\"" /usr/local/php/etc/php.ini
    /etc/init.d/php-fpm restart

    if [ -s /sbin/iptables ]; then
        /sbin/iptables -I INPUT -p tcp -s 127.0.0.1 --dport 11211 -j ACCEPT
        /sbin/iptables -I INPUT -p udp -s 127.0.0.1 --dport 11211 -j ACCEPT
        /sbin/iptables -I INPUT -p tcp --dport 11211 -j DROP
        /sbin/iptables -I INPUT -p udp --dport 11211 -j DROP
        if [ "$PM" = "yum" ]; then
            service iptables save
         elif [ "$PM" = "apt" ]; then
            iptables-save > /etc/iptables.rules
        fi
    fi

    echo "Starting Memcached..."
    /etc/init.d/memcached start

    if [ -s ${zend_ext} ]; then
        echo "====== Memcached install completed ======"
        echo "Memcached installed successfully, enjoy it!"
     else
        sed -i '/${PHP_ZTS}/d' /usr/local/php/etc/php.ini
        echo "Memcached install failed!"
    fi
}

Install_PHPMemcached()
{
    echo "Install memcached php extension..."
    cd ${cur_dir}/src
    if [ "$PM" = "yum" ]; then
        yum install cyrus-sasl-devel -y
     elif [ "$PM" = "apt" ]; then
        apt-get install libsasl2-2 sasl2-bin libsasl2-2 libsasl2-dev libsasl2-modules -y
    fi
    wget -cN ${Download_Mirror}/libmemcached/${Libmemcached_Ver}.tar.gz
    Libmemcached_Ver1=$( echo ${Libmemcached_Ver} | awk -F"-" '{print $2}' | awk -F"." '{print $1}' )
    Libmemcached_Ver2=$( echo ${Libmemcached_Ver} | awk -F"-" '{print $2}' | awk -F"." '{print $2}' )
    Libmemcached_Ver3=$( echo ${Libmemcached_Ver} | awk -F"-" '{print $2}' | awk -F"." '{print $3}' )
    [[ ! -f ${Libmemcached_Ver}.tgz ]] && wget -c https://launchpad.net/libmemcached/${Libmemcached_Ver1}.${Libmemcached_Ver2}/${Libmemcached_Ver1}.${Libmemcached_Ver2}.${Libmemcached_Ver3}/+download/${Libmemcached_Ver}.tar.gz
    tar zxf ${Libmemcached_Ver}.tar.gz
    cd ${Libmemcached_Ver}
    ./configure --prefix=/usr/local/libmemcached --with-memcached
    make && make install
    rm -fr ${cur_dir}/src/${Libmemcached_Ver}

    cd ${cur_dir}/src
    wget -cN ${Download_Mirror}/php-memcached/php-memcached-php7.zip
    [[ ! -s php-memcached-php7.zip ]] && wget -c https://github.com/php-memcached-dev/php-memcached/archive/php7.zip -O php-memcached-php7.zip
    if [ -s php-memcached-php7.zip ]; then
        unzip -o php-memcached-php7.zip
        cd php-memcached-php7
    else
        rm -rf php-memcached
        git clone -b php7 https://github.com/php-memcached-dev/php-memcached.git
        cd php-memcached
    fi
    /usr/local/php/bin/phpize
    ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcached --with-libmemcached-dir=/usr/local/libmemcached
    make && make install
    rm -fr ${cur_dir}/src/php-memcached*
}